import Vue from 'vue'
import Vuex from 'vuex'

import edu from './module/edu'
import compet from './module/compet'
import board from './module/board'
import user from './module/user'
import style from './module/style'
import login from './module/login'
import {common} from './module/common'
import designer_menu_config from '@/router/designer_menu';
Vue.use(Vuex)
const projectName = sessionStorage.getItem('projectName');
export default new Vuex.Store({
  state: {
    //
    projectName:projectName || '14',
    cloudType: 'common',
    currentTime: new Date().getTime(),
    menuInfo:{}, //当前菜单
    routeList: [],
    flatList:[], // 扁平化菜单
    detailBreadList: [], //面包屑导航
    hasAddBreadItem:false
  },
  getters:{
    pageTitle(state) {
        return state.menuInfo ? state.menuInfo.title : '';
    },
    pageName(state) {
        return state.menuInfo ? state.menuInfo.name : '';
    },
    breadList(state) {
      let navFirst = {
          name: state.menuInfo ? state.menuInfo.title : '',
          to: {
              name: state.menuInfo ? state.menuInfo.name : ''
          }
      };
      return [navFirst, ...state.detailBreadList];
    },
    getMenuName: (state) => (path) => {
      let menu  = state.flatList.find(item => item.name === path)
      return menu && menu.title || '';
    },
    //有竞赛
    hasCompete(state) {
        return state.flatList.some((item) => item.name === 'competitionScene');
    },
    //有演练
    hasDrill(state) {
        return state.flatList.some((item) => ['drillScene'].includes(item.name));
    },
    hasFz(state){
      return state.flatList.some((item) => ['fztest'].includes(item.name));
    },
    hasTrain(state) {
        return state.flatList.some((item) => item.name === 'schedulePlan');
    },
    //有考试
    hasExam(state) {
        return state.flatList.some((item) => item.name === 'exam');
    },
    //有ctf
    hasCtf(state) {
      return state.flatList.some((item) => item.name === 'ctfContest');
    },
    // 有渗透
    hasST(state) {
        return state.flatList.some((item) => item.name === 'stDrill');
    },
    // 有攻防
    hasAWD(state) {
        return state.flatList.some((item) => item.name === 'awdContest');
    },
    // 攻防pulus
    hasAWDPlus(state){
      return state.flatList.some((item) => item.name === 'awdPlusContest');
    },
    //有知识赛
    hasKnowledge(state) {
        return state.flatList.some((item) => item.name === 'kcContest');
    },
    //有培训
    hasClass(state) {
        return state.flatList.some((item) => item.name === 'class');
    },
    //有应急
    hasYJ(state){
      return state.flatList.some((item) => item.name === 'emergencyDefenseDril')
    },
    //综合
    hasZH(state){
      return state.flatList.some((item) => item.name === 'multipleDrill');
    },
    //运维赛
    hasYWC(state){
      return state.flatList.some((item) => item.name === 'opContest');
    },
    showDrillAndCompete(state) { //系统中显示的竞赛演练列表，目前用于首页画图，和归档页
      let competeAndDrill = [
        { name: 'awdContest', type: 1},
        { name: 'awdPlusContest', type: 8},
        { name: 'ctfContest', type: 2},
        { name: 'kcContest', type: 3},
        { name: 'awdDrill', type: 0},
        { name: 'stDrill', type: 4},
        { name: 'multipleDrill', type: 7},
        { name: 'hwDrill', type: 5},
        { name: 'swDrill', type: 14},
        { name: 'opContest', type: 9},
        { name:'emergencyDefenseDrill',type:10},
        { name:'zcDrill',type:11},
        { name:'dailyDrill',type:12},
        { name:'bigDrill',type:13},
        { name:'fzDrill',type:15}
      ];
      return competeAndDrill
        .map((defItem) => {
          let menu = state.flatList.find((item) => item.name == defItem.name && !item.hide && designer_menu_config[item.name]);
          return menu ? Object.assign(defItem, menu) : '';
        })
        .filter((item) => item);
    }
  },
  mutations: {
    //
    setProjectName(state,name){
    	sessionStorage.setItem('projectName',name)
    	state.projectName = name
    },
    setCloudType(state,val) {
      state.cloudType = val;
    },
    setCurrentTime(state, time) {
      state.currentTime = time
    },
    setRouteList(state, list){
      // console.log('菜单列表有层级',list);
      state.routeList = list
    },
    setflatList(state, list){
      // console.log('扁平化菜单',list);
      state.flatList = list
    },
    setBreadList(state, info) {
        state.breadList = info;
    },
    addBreadItem(state, info) {
        // console.log('添加面包屑');
        let breadList = state.detailBreadList;
        let index = breadList.findIndex((item) => item.name === info.name);
        if (index > -1) {
            breadList.splice(index,1,info);
        } else {
            breadList.push(info);
        }
        state.detailBreadList = breadList;
        state.hasAddBreadItem = true;
    },
    removeBreadItem(state, count) {
        // console.log('移除面包屑');
        state.detailBreadList.splice(-count);
        if(state.detailBreadList.length === 0){
          state.hasAddBreadItem = false;
        }
    },
    clearBreadList(state) {
        state.detailBreadList = [];
        state.hasAddBreadItem = false;
        // console.log('清除面包屑');
    },
    setMenuInfo(state, info) {
      if (!info) {
          return;
      }
      if (state.menuInfo && state.menuInfo.name && !state.hasAddBreadItem || (state.menuInfo && state.menuInfo.name && state.menuInfo.name !== info.name)) {
          // console.log('清空面包屑');
          state.detailBreadList = [];
          state.hasAddBreadItem = false;
      }
      state.menuInfo = info;
    }
  },
  actions: {
    //
  },
  modules: {
    common,
    compet,
    board,
    user,
    style,
    login
  }
})
